package com.bcn.login_mybatisplus_demo.saraca.service.impl;


import com.bcn.login_mybatisplus_demo.interceptor.AuthStorage;
import com.bcn.login_mybatisplus_demo.pojo.vo.R;
import com.bcn.login_mybatisplus_demo.saraca.pojo.AppUser;
import com.bcn.login_mybatisplus_demo.saraca.mapper.AppUserMapper;
import com.bcn.login_mybatisplus_demo.saraca.service.IAppUserService;
import com.bcn.login_mybatisplus_demo.saraca.vo.AppUserVo;
import com.bcn.login_mybatisplus_demo.util.DateUtils;
import com.bcn.login_mybatisplus_demo.util.StringUtils;
import com.bcn.login_mybatisplus_demo.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * app用户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-28
 */
@Service
public class AppUserServiceImpl implements IAppUserService {
    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 查询app用户
     *
     * @param id app用户主键
     * @return app用户
     */
    @Override
    public AppUser selectAppUserById(Long id) {
        return appUserMapper.selectAppUserById(id);
    }

    /**
     * 查询app用户列表
     *
     * @param appUser app用户
     * @return app用户
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser) {
        appUser.setId(AuthStorage.getUser().getId());
        return appUserMapper.selectAppUserList(appUser);
    }

    /**
     * 新增app用户
     *
     * @param appUser app用户
     * @return 结果
     */
    @Override
    public R insertAppUser(AppUser appUser) {

        AppUser tempUser = new AppUser();
        tempUser.setAccount(appUser.getAccount());
        List<AppUser> appUsers = appUserMapper.selectAppUserList(tempUser);
        if (!appUsers.isEmpty()) {
            return R.error("该账号已经存在" + appUser.getAccount());
        }

        appUser.setCreateTime(DateUtils.getNowDate());
        appUser.setUserName("默认用户");
        return R.success(appUserMapper.insertAppUser(appUser));
    }

    /**
     * 修改app用户
     *
     * @param appUser app用户
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser) {
        return appUserMapper.updateAppUser(appUser);
    }

    /**
     * 批量删除app用户
     *
     * @param ids 需要删除的app用户主键
     * @return 结果
     */
    @Override
    public int deleteAppUserByIds(Long[] ids) {
        return appUserMapper.deleteAppUserByIds(ids);
    }

    /**
     * 删除app用户信息
     *
     * @param id app用户主键
     * @return 结果
     */
    @Override
    public int deleteAppUserById(Long id) {
        return appUserMapper.deleteAppUserById(id);
    }

    @Override
    public R login(AppUser user) {

        AppUser appUser1 = appUserMapper.selectAppUser(user);
        if (appUser1 == null) {
            return R.error("该账户不存在 请注册!" + user.getAccount());
        }


        List<AppUser> appUsers = appUserMapper.selectAppUserList(user);
        if (appUsers.isEmpty()) {
            return R.error("请输入正确的账号或者密码");
        }


        String token = TokenUtils.getToken(appUsers.get(0).getId() + "", appUsers.get(0).getPassword());
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("userInfo", appUsers.get(0));
        return R.success(data);
    }
}
